package com.sxt.practiceapplication.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.entity.studyrecord.StudyRecord;
import com.sxt.practiceapplication.utils.DisplayUtil;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

public abstract class BasePager {

    /**
     * 上下文
     * MainActivity2
     */
    protected final Context context;

    /**
     * 代表各个不同的页面
     */
    public View rootView;

    /**
     * 是否已经初始化数据
     */
    public boolean isInitData;

    public float screenDensity;

    /**
     * 标题
     */
    @SuppressLint("NonConstantResourceId")
    @ViewInject(value = R.id.head_title)
    public TextView headTitle;

    /**
     * 更多操作
     */
    @SuppressLint("NonConstantResourceId")
    @ViewInject(value = R.id.more_operator)
    public View moreOperator;

    /**
     * 加载子页面
     */
    @SuppressLint("NonConstantResourceId")
    @ViewInject(value = R.id.fl_content)
    public FrameLayout content;

    private OnMoreClickListener onMoreClickListener;

    public BasePager(Context context) {
        this.context = context;
        this.rootView = initView();
    }

    /**
     * 用于初始化公共部分视图
     *
     * @return {@link View}
     */
    protected View initView() {
        View pager = View.inflate(context, R.layout.base_pager_2, null);
        screenDensity = DisplayUtil.getScreenDensity(context);
        x.view().inject(this, pager);
        initContent(content);
        initMoreOperator();
        return pager;
    }

    /**
     * 初始化fl_content部分
     *
     * @param content 加载子页面
     */
    protected void initContent(FrameLayout content) {

    }


    /**
     * 初始化数据
     */
    public void initData() {
        if (!isInitData) {
            doInitData();
            this.isInitData = true;
        }
    }

    public void doInitData() {

    }

    public void reloadData() {
        initContent(content);
        doInitData();
        this.isInitData = true;
    }

    private void initMoreOperator() {
        if (onMoreClickListener != null) {
            this.moreOperator.setVisibility(View.VISIBLE);
            this.moreOperator.setOnClickListener(v -> onMoreClickListener.onClick(v));
        } else {
            this.moreOperator.setVisibility(View.INVISIBLE);
        }
    }

    public void setOnMoreClickListener(OnMoreClickListener listener) {
        this.onMoreClickListener = listener;
    }

    public interface OnMoreClickListener {
        void onClick(View view);
    }

}
